package cn.wolfcode.mp.web;

import cn.wolfcode.mp.domain.Employee;
import cn.wolfcode.mp.mapper.EmployeeMapper;
import cn.wolfcode.mp.service.IEmployeeService;
import cn.wolfcode.util.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("employees")
public class EyployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    public List<Employee> list(){
        return employeeService.list();
    }

    @GetMapping("/detail")
    public Employee detail(Long id){
        return employeeService.getById(id);
    }

    @PostMapping("/save")
    public JsonResult save(Employee employee){
        employeeService.save(employee);
        return  JsonResult.success();
    }

    @PutMapping("/update")
    public JsonResult update(Employee employee){
        UpdateWrapper<Employee> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",employee.getId())
                .set("name",employee.getName())
                .set("age",employee.getAge());
        employeeService.update(wrapper);
        return JsonResult.success();
    }

    @DeleteMapping("/deleteId")
    public JsonResult deleteId(Long id){
        employeeService.removeById(id);
        return JsonResult.success();
    }



}
